package gate

import (
	"github.com/gin-gonic/gin"
	"iot-base/auth/api/logic/gatelogic"
	"iot-base/auth/svc"
	"iot-base/auth/types"
	"strconv"
	"strings"
)

func AddCard(c *gin.Context) {
	resp := types.NewResult(c)
	var req types.ReqAddCard
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	list, _ := svc.Dao.TGateCard.FindCardByCode(req.CardCode)
	if len(list) > 0 {
		resp.ErrorCode(types.Fail, "请勿添加相同卡片！")
		return
	}
	var logic gatelogic.BaseGate
	logic = gatelogic.NewGate()
	err = logic.AddCard(req)
	if err != nil {
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	resp.Success(gin.H{})
	return
}

func DelCard(c *gin.Context) {
	resp := types.NewResult(c)
	var req types.ReqDelCard
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	var logic gatelogic.BaseGate
	logic = gatelogic.NewGate()
	err = logic.DelCard(req)
	if err != nil {
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	resp.Success(gin.H{})
	return
}

func CardList(c *gin.Context) {
	resp := types.NewResult(c)
	var req types.ReqQueryCard
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types.ParamsError, "")
		return
	}

	strArr := strings.Split(req.Ids, ",")
	ids := make([]int64, 0, len(strArr))
	for _, v := range strArr {
		id, _ := strconv.ParseInt(v, 10, 64)
		ids = append(ids, id)
	}
	list, err := svc.Dao.TGateCard.FindByIds(ids)
	if err != nil {
		resp.ErrorCode(types.Fail, err.Error())
		return
	}
	resp.Success(list)
	return
}
